/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medusa.gruul.common.core.exception.ServiceException;
import com.medusa.gruul.common.core.util.PageUtils;
import com.medusa.gruul.platform.api.entity.AccountInfo;
import com.medusa.gruul.platform.api.entity.AgentInfo;
import com.medusa.gruul.platform.api.entity.AgentPackageOrder;
import com.medusa.gruul.platform.api.entity.SysShopPackageOrder;
import com.medusa.gruul.platform.mapper.AgentPackageOrderMapper;
import com.medusa.gruul.platform.model.dto.OrderOptionDto;
import com.medusa.gruul.platform.model.dto.agent.AgentPackageOrderDto;
import com.medusa.gruul.platform.model.vo.agent.AgentPackageOrdersVo;
import com.medusa.gruul.platform.service.IAccountInfoService;
import com.medusa.gruul.platform.service.IAgentPackageOrderService;
import com.medusa.gruul.platform.service.ISysShopPackageOrderService;
import com.medusa.gruul.platform.stp.StpAgentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 代理分润的套餐
 * </p>
 *
 * @author whh
 * @since 2020-11-06
 */
@Service
public class AgentPackageOrderServiceImpl extends ServiceImpl<AgentPackageOrderMapper, AgentPackageOrder> implements IAgentPackageOrderService {


    @Autowired
    private ISysShopPackageOrderService sysShopPackageOrderService;
    @Autowired
    private IAccountInfoService accountInfoService;

    @Override
    public void insertPakcageOrder(SysShopPackageOrder order, Integer status, BigDecimal earnProfitMoney, AgentInfo agentInfo) {
        AgentPackageOrder agentPackageOrder = this.baseMapper.selectOne(new QueryWrapper<AgentPackageOrder>()
                .eq("order_num", order.getOrderNum())
                .eq("agent_id",order.getAgentId())
        );
        if (agentPackageOrder == null) {
            agentPackageOrder = new AgentPackageOrder();
            agentPackageOrder.setAgentId(order.getAgentId());
            agentPackageOrder.setOrderNum(order.getOrderNum());
            agentPackageOrder.setAccountId(order.getAccountId());
            agentPackageOrder.setPayType(order.getPayType());
            agentPackageOrder.setPayTime(order.getCreateTime());
            agentPackageOrder.setPaidPayable(order.getPaidPayable());
            agentPackageOrder.setPackageId(order.getPackageId());
            agentPackageOrder.setPackageData(order.getPackageData());
            agentPackageOrder.setStatus(status);
            agentPackageOrder.setAgentId(agentInfo.getId());
            agentPackageOrder.setEarnProfitMoney(earnProfitMoney);
            agentPackageOrder.setOrderSource(order.getOrderSource());
            this.save(agentPackageOrder);
        }

    }

    @Override
    public void updateByOrderNumber(String orderNum, Integer status) {
        this.update(new UpdateWrapper<AgentPackageOrder>().set("status", status).eq("order_num", orderNum));
    }

    @Override
    public PageUtils<AgentPackageOrdersVo> packageOrders(Integer page, Integer size, String startTime, String endTime, String search, String payType, Integer status) {

        IPage<AgentPackageOrderDto> resultIpage = this.baseMapper.selectByCondition(new Page(page, size), startTime, endTime, search, payType, status, StpAgentUtil.getLoginIdAsLong());
        // 封装返回对象
        PageUtils<AgentPackageOrdersVo> result = new PageUtils<>(null, (int) resultIpage.getTotal(), (int) resultIpage.getSize(), (int) resultIpage.getCurrent());
        if (CollectionUtil.isEmpty(resultIpage.getRecords())) {
            return result;
        }
        List<AgentPackageOrdersVo> infosVos = resultIpage.getRecords().stream().map(obj -> {
            return BeanUtil.toBean(obj, AgentPackageOrdersVo.class);
        }).collect(Collectors.toList());
        result.setList(infosVos);
        return result;

    }

    @Override
    public PageUtils<AgentPackageOrdersVo> agentOrders(Integer page, Integer size, Integer status, String startTime, String endTime, String phone,
                                                       String orderNum, String nikeName, Integer payType, Long templateId) {
        IPage<AgentPackageOrderDto> resultIpage = this.baseMapper.selectAgentOrders(new Page<>(page, size),
                status, startTime, endTime, phone, orderNum, nikeName, payType, templateId);
        // 封装返回对象
        PageUtils<AgentPackageOrdersVo> result = new PageUtils<>(null, (int) resultIpage.getTotal(), (int) resultIpage.getSize(), (int) resultIpage.getCurrent());
        if (CollectionUtil.isEmpty(resultIpage.getRecords())) {
            return result;
        }

        List<AgentPackageOrdersVo> infosVos = resultIpage.getRecords().stream().map(obj -> {
            SysShopPackageOrder order = sysShopPackageOrderService.getByOrderNum(obj.getOrderNum());
            AgentPackageOrdersVo agentPackageOrdersVo = BeanUtil.toBean(obj, AgentPackageOrdersVo.class);
            agentPackageOrdersVo.setPayInfo(order.getPayInfo());
            AccountInfo accountInfo = accountInfoService.getById(obj.getAccountId());
            agentPackageOrdersVo.setAvatarUrl(accountInfo.getAvatarUrl());
            return agentPackageOrdersVo;
        }).collect(Collectors.toList());
        result.setList(infosVos);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void packageOrderOption(OrderOptionDto dto) {
        AgentPackageOrder agentPackageOrder = getById(dto.getOrderId());
        if (agentPackageOrder == null) {
            throw new ServiceException("订单不存在");
        }
        SysShopPackageOrder sysShopPackageOrder = sysShopPackageOrderService.getOne(new QueryWrapper<SysShopPackageOrder>()
                .eq("order_num", agentPackageOrder.getOrderNum()));
        dto.setOrderId(sysShopPackageOrder.getId());
        sysShopPackageOrderService.orderOption(dto);
    }
}
